package vitamin.demo.certification.service.impl;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import vitamin.demo.certification.diyexception.MisRequestException;
import vitamin.demo.certification.mapper.AdminMapper;
import vitamin.demo.certification.service.AdminService;
import vitamin.demo.utils.LoginUtils;
import vitamin.demo.pojo.User;
import vitamin.demo.utils.ResultUtils;
import vitamin.demo.utils.Utils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author HAC
 */
@Primary
@Service
@Slf4j
public class AdminServiceImpl implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Resource
    RedisTemplate redisTemplate;

    @NonNull
    public User vertify(User user,String password) {

        Assert.notNull(user,"用户参数为空或用户不存在");

//        String username=user.getUsername();

        String correctPassword=user.getPassword();

        String errorTip="用户名或密码不正确";

        if(!(passwordMatch(user,password,correctPassword))){
            log.error("登录参数不正确");
            throw new MisRequestException(errorTip);
        }
        return user;
    }


    public boolean passwordMatch(User user,String password,String correctPassword) {
        Assert.notNull(user, "用户参数不为空");
        return !LoginUtils.isBlank(password)&& LoginUtils.checkPassword(password,correctPassword);
    }

    public User getUserByusername(String username) {
        String errorTip="无法获取用户信息";

        try{
            return adminMapper.getUserByusername(username);
        }catch (Exception e){
            log.error("无法通过用户名"+username+"获得用户信息");
            throw new MisRequestException(errorTip);
        }
    }

    @Override
    @Cacheable(value = "token_id",key = "'buildToken'+#username")
    public String buildToken(String username) {
        String token_id=Utils.getRandCode(20);

        adminMapper.buildToken(token_id,username);//token写入数据库

        log.info("用户"+username+"生成token_id"+":"+token_id);

        return token_id;
    }

    @Override
    public boolean register(String username, String password, String nickname) {

        adminMapper.register(username,password,nickname);

        log.info("注册用户"+username);

        return true;

    }

    @Override
    public String typeCheck(HttpServletRequest request) {

        String token =request.getHeader("token");

        User user=(User)redisTemplate.opsForValue().get(token);

        if(user!=null){
            if(user.getUser_type()=="0"){
                return ResultUtils.doSuccess();
            }else {
                return ResultUtils.doFail();
            }
        }
        return null;
    }

}
